﻿using System;
using System.Collections.Generic;
using PlanningGIS.MetadataEdit;
using System.Text;



namespace PlanningGIS.MetadataEdit
{

    public class MapNoUtility
    {
        /// <summary>
        /// 校验大比例尺图号的正确性
        /// </summary>
        /// <param name="strTFBH">图号</param>
        /// <param name="dblLeft">图号的左侧数字</param>
        /// <param name="dblRight">图号的右侧数字</param>
        /// <returns></returns>
        public static bool CheckTfbh500(string strTFBH, out float dblLeft, out float dblRight)
        {
            dblLeft = 0; dblRight = 0;
            strTFBH = strTFBH.Replace(" ", "");
            bool bln = strTFBH.StartsWith("-");
            if (bln)
            {
                strTFBH = strTFBH.Substring(1);
            }
            int index = strTFBH.IndexOf("-");
            if (index < 1)
            {
                return false;
            }
            if (!float.TryParse(strTFBH.Substring(0, index), out dblLeft))
            {
                return false;
            }
            index = strTFBH.IndexOf("-");
            if (!float.TryParse(strTFBH.Substring(index + 1), out dblRight))
            {
                return false;
            }
            if (bln)
                dblLeft = dblLeft * (-1);
            return true;
        }

        /// <summary>
        /// 校验1：5000比例尺图幅的图号的正确性
        /// </summary>
        /// <param name="strTFBH">图号</param>
        /// <param name="rowNoMillion">图幅对应1：100万图幅的行号</param>
        /// <param name="strColNoMillion">图幅对应1：100万图幅的列号</param>
        /// <param name="strRowNo">图幅的行号</param>
        /// <param name="strColNo">图幅的列号</param>
        /// <param name="dblLong">经度</param>
        /// <param name="dblLat">纬度</param>
        /// <returns></returns>
        public static bool CheckTfbh5000(string strTFBH, out char rowNoMillion, out string strColNoMillion, out string strRowNo, out string strColNo, out double dblLong, out double dblLat)
        {
            dblLong = 0;
            dblLat = 0;
            rowNoMillion = 'H';
            strColNoMillion = "50";
            strRowNo = "001";
            strColNo = "001";
            if (strTFBH.Length != 10) return false;
            strTFBH = strTFBH.ToUpper();

            rowNoMillion = strTFBH[0]; //1：100万比例尺的行号
            if (!(rowNoMillion >= 'A' & rowNoMillion <= 'V')) return false;

            strColNoMillion = strTFBH.Substring(1, 2); //1：100万比例尺的列号
            int colNoMillion;
            if (!int.TryParse(strColNoMillion, out colNoMillion)) return false;
            if (!(colNoMillion >= 1 & colNoMillion <= 60)) return false;

            char scale = strTFBH[3]; //比例尺
            if (scale != 'H') return false;

            strRowNo = strTFBH.Substring(4, 3); //行号
            int rowNo;
            if (!int.TryParse(strRowNo, out rowNo)) return false;
            if (!(rowNo >= 1 & rowNo <= 192)) return false;

            strColNo = strTFBH.Substring(7, 3); //列号
            int colNo;
            if (!int.TryParse(strColNo, out colNo)) return false;
            if (!(colNo >= 1 & colNo <= 192)) return false;

            double width = 6.0 / 192;  //经差
            double height = 4.0 / 192; //纬差
            dblLong = (colNoMillion - 31) * 6.0 + (colNo - 1) * width;// 经度
            dblLat = (rowNoMillion - 'A') * 4.0 + (4.0 / height - rowNo) * height; //纬度
            return true;
        }

        /// <summary>
        /// 校验1：10000比例尺图幅的图号的正确性
        /// </summary>
        /// <param name="strTFBH">图号</param>
        /// <param name="rowNoMillion">图幅对应1：100万图幅的行号</param>
        /// <param name="strColNoMillion">图幅对应1：100万图幅的列号</param>
        /// <param name="strRowNo">图幅的行号</param>
        /// <param name="strColNo">图幅的列号</param>
        /// <param name="dblLong">经度</param>
        /// <param name="dblLat">纬度</param>
        /// <returns></returns>
        public static bool CheckTfbh10000(string strTFBH, out char rowNoMillion, out string strColNoMillion, out string strRowNo, out string strColNo, out double dblLong, out double dblLat)
        {
            dblLong = 0;
            dblLat = 0;
            rowNoMillion = 'H';
            strColNoMillion = "50";
            strRowNo = "001";
            strColNo = "001";
            if (strTFBH.Length != 10) return false;
            strTFBH = strTFBH.ToUpper();

            rowNoMillion = strTFBH[0]; //1：100万比例尺的行号
            if (!(rowNoMillion >= 'A' & rowNoMillion <= 'V')) return false;

            strColNoMillion = strTFBH.Substring(1, 2); //1：100万比例尺的列号
            int colNoMillion;
            if (!int.TryParse(strColNoMillion, out colNoMillion)) return false;
            if (!(colNoMillion >= 1 & colNoMillion <= 60)) return false;

            char scale = strTFBH[3]; //比例尺
            if (scale != 'G') return false;

            strRowNo = strTFBH.Substring(4, 3); //行号
            int rowNo;
            if (!int.TryParse(strRowNo, out rowNo)) return false;
            if (!(rowNo >= 1 & rowNo <= 96)) return false;

            strColNo = strTFBH.Substring(7, 3); //列号
            int colNo;
            if (!int.TryParse(strColNo, out colNo)) return false;
            if (!(colNo >= 1 & colNo <= 96)) return false;

            double width = 6.0 / 96;  //经差
            double height = 4.0 / 96; //纬差
            dblLong = (colNoMillion - 31) * 6.0 + (colNo - 1) * width;// 经度
            dblLat = (rowNoMillion - 'A') * 4.0 + (4.0 / height - rowNo) * height; //纬度
            return true;
        }
        /// <summary>
        /// 校验1：50000比例尺图幅的图号的正确性
        /// </summary>
        /// <param name="strTFBH">图号</param>
        /// <param name="rowNoMillion">图幅对应1：100万图幅的行号</param>
        /// <param name="strColNoMillion">图幅对应1：100万图幅的列号</param>
        /// <param name="strRowNo">图幅的行号</param>
        /// <param name="strColNo">图幅的列号</param>
        /// <param name="dblLong">经度</param>
        /// <param name="dblLat">纬度</param>
        /// <returns></returns>
        public static bool CheckTfbh50000(string strTFBH, out char rowNoMillion, out string strColNoMillion, out string strRowNo, out string strColNo, out double dblLong, out double dblLat)
        {
            dblLong = 0;
            dblLat = 0;
            rowNoMillion = 'H';
            strColNoMillion = "50";
            strRowNo = "001";
            strColNo = "001";
            if (strTFBH.Length != 10) return false;
            strTFBH = strTFBH.ToUpper();

            rowNoMillion = strTFBH[0]; //1：100万比例尺的行号
            if (!(rowNoMillion >= 'A' & rowNoMillion <= 'V')) return false;

            strColNoMillion = strTFBH.Substring(1, 2); //1：100万比例尺的列号
            int colNoMillion;
            if (!int.TryParse(strColNoMillion, out colNoMillion)) return false;
            if (!(colNoMillion >= 1 & colNoMillion <= 60)) return false;

            char scale = strTFBH[3]; //比例尺
            if (scale != 'E') return false;

            strRowNo = strTFBH.Substring(4, 3); //行号
            int rowNo;
            if (!int.TryParse(strRowNo, out rowNo)) return false;
            if (!(rowNo >= 1 & rowNo <= 24)) return false;

            strColNo = strTFBH.Substring(7, 3); //列号
            int colNo;
            if (!int.TryParse(strColNo, out colNo)) return false;
            if (!(colNo >= 1 & colNo <= 24)) return false;

            double width = 6.0 / 24;  //经差
            double height = 4.0 / 24; //纬差
            dblLong = (colNoMillion - 31) * 6.0 + (colNo - 1) * width;// 经度
            dblLat = (rowNoMillion - 'A') * 4.0 + (4.0 / height - rowNo) * height; //纬度
            return true;
        }
        /// <summary>
        /// 校验1：250000比例尺图幅的图号的正确性
        /// </summary>
        /// <param name="strTFBH">图号</param>
        /// <param name="rowNoMillion">图幅对应1：100万图幅的行号</param>
        /// <param name="strColNoMillion">图幅对应1：100万图幅的列号</param>
        /// <param name="strRowNo">图幅的行号</param>
        /// <param name="strColNo">图幅的列号</param>
        /// <param name="dblLong">经度</param>
        /// <param name="dblLat">纬度</param>
        /// <returns></returns>
        public static bool CheckTfbh250000(string strTFBH, out char rowNoMillion, out string strColNoMillion, out string strRowNo, out string strColNo, out double dblLong, out double dblLat)
        {
            dblLong = 0;
            dblLat = 0;
            rowNoMillion = 'H';
            strColNoMillion = "50";
            strRowNo = "001";
            strColNo = "001";
            if (strTFBH.Length != 10) return false;
            strTFBH = strTFBH.ToUpper();

            rowNoMillion = strTFBH[0]; //1：100万比例尺的行号
            if (!(rowNoMillion >= 'A' & rowNoMillion <= 'V')) return false;

            strColNoMillion = strTFBH.Substring(1, 2); //1：100万比例尺的列号
            int colNoMillion;
            if (!int.TryParse(strColNoMillion, out colNoMillion)) return false;
            if (!(colNoMillion >= 1 & colNoMillion <= 60)) return false;

            char scale = strTFBH[3]; //比例尺
            if (scale != 'C') return false;

            strRowNo = strTFBH.Substring(4, 3); //行号
            int rowNo;
            if (!int.TryParse(strRowNo, out rowNo)) return false;
            if (!(rowNo >= 1 & rowNo <= 4)) return false;

            strColNo = strTFBH.Substring(7, 3); //列号
            int colNo;
            if (!int.TryParse(strColNo, out colNo)) return false;
            if (!(colNo >= 1 & colNo <= 4)) return false;

            double width = 6.0 / 4;  //经差
            double height = 4.0 / 4; //纬差
            dblLong = (colNoMillion - 31) * 6.0 + (colNo - 1) * width;// 经度
            dblLat = (rowNoMillion - 'A') * 4.0 + (4.0 / height - rowNo) * height; //纬度
            return true;
        }

        /// <summary>
        /// 获取大比例尺图号的相邻8个图号,水平顺序读取，返回的是一个长度为9的数组（数组的中间为传入的图号本身）
        /// </summary>
        /// <param name="strTfbh">传入的图号</param>
        /// <param name="scale">比例尺分母</param>
        /// <param name="blnYX">图号命名是是否Y-X</param>
        /// <param name="bln4X5">图幅规格是否是4*5的</param>
        /// <returns></returns>
        public static string[] GetAdjacentTFBH(string strTfbh, int scale, bool blnYX, bool bln4X5)
        {
            float dblLeft, dblRight;
            string[] strs = new string[9];
            if (!CheckTfbh500(strTfbh, out dblLeft, out dblRight)) return null;
            double dblHeight, dblWidth;
            int precision = 2;
            switch (scale)
            {
                case 500:
                    dblWidth = 250;
                    dblHeight = bln4X5 ? 200 : 250;
                    precision = 2;
                    break;
                case 1000:
                    dblWidth = 500;
                    dblHeight = bln4X5 ? 400 : 500;
                    precision = 1;
                    break;
                case 2000:
                    dblWidth = 1000;
                    dblHeight = bln4X5 ? 800 : 1000;
                    precision = 0;
                    break;
                case 5000:
                    dblWidth = 2500;
                    dblHeight = bln4X5 ? 2000 : 2500;
                    precision = 1;
                    break;
                case 10000:
                    dblWidth = 500;
                    dblHeight = bln4X5 ? 4000 : 5000;
                    precision = 0;
                    break;
                default:
                    {
                        return null;
                    }
            }
            strs[0] = blnYX ?
            (dblLeft + dblHeight / 1000).ToString("F" + precision) + "-" + (dblRight - dblWidth / 1000).ToString("F" + precision) :
            (dblLeft - dblWidth / 1000).ToString("F" + precision) + "-" + (dblRight + dblHeight / 1000).ToString("F" + precision);

            strs[1] = blnYX ?
            (dblLeft + dblHeight / 1000).ToString("F" + precision) + "-" + (dblRight).ToString("F" + precision) :
            (dblLeft).ToString("F" + precision) + "-" + (dblRight + dblHeight / 1000).ToString("F" + precision);

            strs[2] = blnYX ?
            (dblLeft + dblHeight / 1000).ToString("F" + precision) + "-" + (dblRight + dblWidth / 1000).ToString("F" + precision) :
            (dblLeft + dblWidth / 1000).ToString("F" + precision) + "-" + (dblRight + dblHeight / 1000).ToString("F" + precision);


            strs[3] = blnYX ?
             (dblLeft).ToString("F" + precision) + "-" + (dblRight - dblWidth / 1000).ToString("F" + precision) :
             (dblLeft - dblWidth / 1000).ToString("F" + precision) + "-" + (dblRight).ToString("F" + precision);

            strs[4] = strTfbh;

            strs[5] = blnYX ?
             (dblLeft).ToString("F" + precision) + "-" + (dblRight + dblWidth / 1000).ToString("F" + precision) :
             (dblLeft + dblWidth / 1000).ToString("F" + precision) + "-" + (dblRight).ToString("F" + precision);

            strs[6] = blnYX ?
             (dblLeft - dblHeight / 1000).ToString("F" + precision) + "-" + (dblRight - dblWidth / 1000).ToString("F" + precision) :
             (dblLeft - dblWidth / 1000).ToString("F" + precision) + "-" + (dblRight - dblHeight / 1000).ToString("F" + precision);

            strs[7] = blnYX ?
             (dblLeft - dblHeight / 1000).ToString("F" + precision) + "-" + (dblRight).ToString("F" + precision) :
             (dblLeft).ToString("F" + precision) + "-" + (dblRight - dblHeight / 1000).ToString("F" + precision);

            strs[8] = blnYX ?
             (dblLeft - dblHeight / 1000).ToString("F" + precision) + "-" + (dblRight + dblWidth / 1000).ToString("F" + precision) :
             (dblLeft + dblWidth / 1000).ToString("F" + precision) + "-" + (dblRight - dblHeight / 1000).ToString("F" + precision);
            return strs;
        }


        /// <summary>
        /// 获取某中小比例尺图号的西图号
        /// </summary>
        /// <param name="strTfbh">图号</param>
        /// <param name="strScale">比例尺1:5000至1：250000</param>
        /// <returns></returns>
        public static string GetWestTfbh(string strTfbh, string strScale)
        {
            int rowColCount = 0;
            string strTfbhWest = "";
            double dblLong, dblLat;
            char rowNoMillion = 'A';
            string strColNoMillion = "", strRowNo = "", strColNo = "";
            int colNo = 0;
            int colNoMillion = 0;
            char scale;
            switch (strScale)
            {
                case "1:5000":
                    rowColCount = 192;
                    scale = 'H';
                    if (!CheckTfbh5000(strTfbh, out  rowNoMillion, out strColNoMillion, out strRowNo, out strColNo, out dblLong, out dblLat)) return "";
                    break;
                case "1:10000":
                    rowColCount = 96;
                    scale = 'G';
                    if (!CheckTfbh10000(strTfbh, out  rowNoMillion, out strColNoMillion, out strRowNo, out strColNo, out dblLong, out dblLat)) return "";
                    break;
                case "1:50000":
                    rowColCount = 24;
                    scale = 'E';
                    if (!CheckTfbh50000(strTfbh, out  rowNoMillion, out strColNoMillion, out strRowNo, out strColNo, out dblLong, out dblLat)) return "";
                    break;
                case "1:250000":
                    rowColCount = 4;
                    scale = 'C';
                    if (!CheckTfbh250000(strTfbh, out  rowNoMillion, out strColNoMillion, out strRowNo, out strColNo, out dblLong, out dblLat)) return "";
                    break;
                default:
                    return "";
            }
            colNo = Convert.ToInt32(strColNo);
            if (colNo == 1)
            {
                colNoMillion = Convert.ToInt32(strColNoMillion);
                if (colNoMillion == 1) return "";
                strTfbhWest = rowNoMillion.ToString() + (colNoMillion - 1) + scale + strRowNo + rowColCount.ToString().PadLeft(3, '0');
            }
            else
            {
                strTfbhWest = rowNoMillion.ToString() + strColNoMillion + scale + strRowNo + (colNo - 1).ToString().PadLeft(3, '0');
            }
            return strTfbhWest;
        }

        /// <summary>
        /// 获取某中小比例尺图号的北图号
        /// </summary>
        /// <param name="strTfbh">图号</param>
        /// <param name="strScale">比例尺1:5000至1：250000</param>
        /// <returns></returns>
        public static string GetNorthTfbh(string strTfbh, string strScale)
        {
            int rowColCount = 0;
            string strTfbhNorth = "";
            double dblLong, dblLat;
            char rowNoMillion;
            string strColNoMillion = "", strRowNo = "", strColNo = "";
            int rowNo;
            char scale;
            switch (strScale)
            {
                case "1:5000":
                    rowColCount = 192;
                    scale = 'H';
                    if (!CheckTfbh5000(strTfbh, out  rowNoMillion, out strColNoMillion, out strRowNo, out strColNo, out dblLong, out dblLat)) return "";
                    break;
                case "1:10000":
                    rowColCount = 96;
                    scale = 'G';
                    if (!CheckTfbh10000(strTfbh, out  rowNoMillion, out strColNoMillion, out strRowNo, out strColNo, out dblLong, out dblLat)) return "";
                    break;
                case "1:50000":
                    rowColCount = 24;
                    scale = 'E';
                    if (!CheckTfbh50000(strTfbh, out  rowNoMillion, out strColNoMillion, out strRowNo, out strColNo, out dblLong, out dblLat)) return "";
                    break;
                case "1:250000":
                    rowColCount = 4;
                    scale = 'C';
                    if (!CheckTfbh250000(strTfbh, out  rowNoMillion, out strColNoMillion, out strRowNo, out strColNo, out dblLong, out dblLat)) return "";
                    break;
                default:
                    return "";
            }
            rowNo = Convert.ToInt32(strRowNo);
            if (rowNo == 1)
            {
                if (rowNoMillion == 'V') return "";
                strTfbhNorth = ((char)(rowNoMillion + 1)).ToString() + strColNoMillion + scale + rowColCount.ToString().PadLeft(3, '0') + strColNo;
            }
            else
            {
                strTfbhNorth = rowNoMillion.ToString() + strColNoMillion + scale + (rowNo - 1).ToString().PadLeft(3, '0') + strColNo;
            }
            return strTfbhNorth;
        }


        /// <summary>
        /// 获取某中小比例尺图号的东图号
        /// </summary>
        /// <param name="strTfbh">图号</param>
        /// <param name="strScale">比例尺1:5000至1：250000</param>
        /// <returns></returns>
        public static string GetEastTfbh(string strTfbh, string strScale)
        {
            int rowColCount = 0;
            string strTfbhEast = "";
            double dblLong, dblLat;
            char rowNoMillion = 'A';
            string strColNoMillion = "", strRowNo = "", strColNo = "";
            int colNo;
            int colNoMillion;
            char scale;
            switch (strScale)
            {
                case "1:5000":
                    rowColCount = 192;
                    if (!CheckTfbh5000(strTfbh, out  rowNoMillion, out strColNoMillion, out strRowNo, out strColNo, out dblLong, out dblLat)) return "";
                    scale = 'G';
                    break;
                case "1:10000":
                    rowColCount = 96;
                    if (!CheckTfbh10000(strTfbh, out  rowNoMillion, out strColNoMillion, out strRowNo, out strColNo, out dblLong, out dblLat)) return "";
                    scale = 'H';
                    break;
                case "1:50000":
                    rowColCount = 24;
                    if (!CheckTfbh50000(strTfbh, out  rowNoMillion, out strColNoMillion, out strRowNo, out strColNo, out dblLong, out dblLat)) return "";
                    scale = 'E';
                    break;
                case "1:250000":
                    rowColCount = 4;
                    if (!CheckTfbh250000(strTfbh, out  rowNoMillion, out strColNoMillion, out strRowNo, out strColNo, out dblLong, out dblLat)) return "";
                    scale = 'C';
                    break;
                default:
                    return "";
            }
            colNo = Convert.ToInt32(strColNo);
            if (colNo == rowColCount)
            {
                colNoMillion = Convert.ToInt32(strColNoMillion);
                if (colNoMillion == 60) return "";
                strTfbhEast = rowNoMillion.ToString() + (colNoMillion + 1).ToString().PadLeft(2, '0') + scale + strRowNo + "001";
            }
            else
            {
                strTfbhEast = rowNoMillion.ToString() + strColNoMillion + scale + strRowNo + (colNo + 1).ToString().PadLeft(3, '0');
            }
            return strTfbhEast;
        }
        /// <summary>
        /// 获取某中小比例尺图号的南图号
        /// </summary>
        /// <param name="strTfbh">图号</param>
        /// <param name="strScale">比例尺1:5000至1：250000</param>
        /// <returns></returns>
        public static string GetSouthTfbh(string strTfbh, string strScale)
        {
            int rowColCount = 0;
            string strTfbhSouth = "";
            double dblLong, dblLat;
            char rowNoMillion;
            string strColNoMillion, strRowNo, strColNo;
            int rowNo;
            char scale;
            switch (strScale)
            {
                case "1:5000":
                    rowColCount = 192;
                    scale = 'H';
                    if (!CheckTfbh5000(strTfbh, out  rowNoMillion, out strColNoMillion, out strRowNo, out strColNo, out dblLong, out dblLat)) return "";
                    break;
                case "1:10000":
                    rowColCount = 96;
                    scale = 'G';
                    if (!CheckTfbh10000(strTfbh, out  rowNoMillion, out strColNoMillion, out strRowNo, out strColNo, out dblLong, out dblLat)) return "";
                    break;
                case "1:50000":
                    rowColCount = 24;
                    scale = 'E';
                    if (!CheckTfbh50000(strTfbh, out  rowNoMillion, out strColNoMillion, out strRowNo, out strColNo, out dblLong, out dblLat)) return "";
                    break;
                case "1:250000":
                    rowColCount = 4;
                    scale = 'C';
                    if (!CheckTfbh250000(strTfbh, out  rowNoMillion, out strColNoMillion, out strRowNo, out strColNo, out dblLong, out dblLat)) return "";
                    break;
                default:
                    return "";
            }
            rowNo = Convert.ToInt32(strRowNo);
            if (rowNo == rowColCount)
            {
                if (rowNoMillion == 'A') return "";
                strTfbhSouth = ((char)(rowNoMillion - 1)).ToString() + strColNoMillion + scale + "001" + strColNo;
            }
            else
            {
                strTfbhSouth = rowNoMillion.ToString() + strColNoMillion + scale + (rowNo + 1).ToString().PadLeft(3, '0') + strColNo;
            }
            return strTfbhSouth;
        }

        /// <summary>
        /// 获取某中小比例尺图号的西北图号
        /// </summary>
        /// <param name="strTfbh">图号</param>
        /// <param name="strScale">比例尺1:5000至1：250000</param>
        /// <returns></returns>
        public static string GetWestNorthTfbh(string strTfbh, string strScale)
        {
            int rowColCount = 0;
            string strTfbhWestNorth = "";
            double dblLong, dblLat;
            char rowNoMillion, rowNoMillion2;
            string strColNoMillion = "", strRowNo = "", strColNo = "";
            string strColNoMillion2 = "", strRowNo2 = "", strColNo2 = "";
            int rowNo, colNo;
            char scale;
            int colNoMillion = 0;
            switch (strScale)
            {
                case "1:5000":
                    rowColCount = 192;
                    scale = 'H';
                    if (!CheckTfbh5000(strTfbh, out  rowNoMillion, out strColNoMillion, out strRowNo, out strColNo, out dblLong, out dblLat)) return "";
                    break;
                case "1:10000":
                    rowColCount = 96;
                    scale = 'G';
                    if (!CheckTfbh10000(strTfbh, out  rowNoMillion, out strColNoMillion, out strRowNo, out strColNo, out dblLong, out dblLat)) return "";
                    break;
                case "1:50000":
                    rowColCount = 24;
                    scale = 'E';
                    if (!CheckTfbh50000(strTfbh, out  rowNoMillion, out strColNoMillion, out strRowNo, out strColNo, out dblLong, out dblLat)) return "";
                    break;
                case "1:250000":
                    rowColCount = 4;
                    scale = 'C';
                    if (!CheckTfbh250000(strTfbh, out  rowNoMillion, out strColNoMillion, out strRowNo, out strColNo, out dblLong, out dblLat)) return "";
                    break;
                default:
                    return "";
            }
            rowNo = Convert.ToInt32(strRowNo);
            colNo = Convert.ToInt32(strColNo);
            if (rowNo == 1)
            {
                if (rowNoMillion == 'V') return "";
                rowNoMillion2 = (char)(rowNoMillion + 1);
                strRowNo2 = rowColCount.ToString().PadLeft(3, '0');
            }
            else
            {
                rowNoMillion2 = rowNoMillion;
                strRowNo2 = (rowNo - 1).ToString().PadLeft(3, '0');
            }
            if (colNo == 1)
            {
                colNoMillion = Convert.ToInt32(strColNoMillion);
                if (colNoMillion == 1) return "";
                strColNoMillion2 = (colNoMillion - 1).ToString().PadLeft(2, '0');
                strColNo2 = rowColCount.ToString();
            }
            else
            {
                strColNoMillion2 = strColNoMillion;
                strColNo2 = (colNo - 1).ToString().PadLeft(3, '0');

            }
            strTfbhWestNorth = rowNoMillion2.ToString() + strColNoMillion2 + scale + strRowNo2 + strColNo2;

            return strTfbhWestNorth;
        }

        /// <summary>
        /// 获取某中小比例尺图号的东北图号
        /// </summary>
        /// <param name="strTfbh">图号</param>
        /// <param name="strScale">比例尺1:5000至1：250000</param>
        /// <returns></returns>
        public static string GetEastNorthTfbh(string strTfbh, string strScale)
        {
            int rowColCount = 0;
            string strTfbhEastNorth = "";
            double dblLong, dblLat;
            char rowNoMillion, rowNoMillion2;
            string strColNoMillion = "", strRowNo = "", strColNo = "";
            string strColNoMillion2 = "", strRowNo2 = "", strColNo2 = "";
            int rowNo, colNo;
            int colNoMillion = 0;
            char scale;
            switch (strScale)
            {
                case "1:5000":
                    rowColCount = 192;
                    scale = 'H';
                    if (!CheckTfbh5000(strTfbh, out  rowNoMillion, out strColNoMillion, out strRowNo, out strColNo, out dblLong, out dblLat)) return "";
                    break;
                case "1:10000":
                    rowColCount = 96;
                    scale = 'G';
                    if (!CheckTfbh10000(strTfbh, out  rowNoMillion, out strColNoMillion, out strRowNo, out strColNo, out dblLong, out dblLat)) return "";
                    break;
                case "1:50000":
                    rowColCount = 24;
                    scale = 'E';
                    if (!CheckTfbh50000(strTfbh, out  rowNoMillion, out strColNoMillion, out strRowNo, out strColNo, out dblLong, out dblLat)) return "";
                    break;
                case "1:250000":
                    rowColCount = 4;
                    scale = 'C';
                    if (!CheckTfbh250000(strTfbh, out  rowNoMillion, out strColNoMillion, out strRowNo, out strColNo, out dblLong, out dblLat)) return "";
                    break;
                default:
                    return "";
            }
            rowNo = Convert.ToInt32(strRowNo);
            colNo = Convert.ToInt32(strColNo);

            if (rowNo == 1)
            {
                if (rowNoMillion == 'V') return "";
                rowNoMillion2 = (char)(rowNoMillion + 1);
                strRowNo2 = rowColCount.ToString().PadLeft(3, '0');
            }
            else
            {
                rowNoMillion2 = rowNoMillion;
                strRowNo2 = (rowNo - 1).ToString().PadLeft(3, '0');
            }
            if (colNo == rowColCount)
            {
                colNoMillion = Convert.ToInt32(strColNoMillion);
                if (colNoMillion == 60) return "";
                strColNoMillion2 = (colNoMillion + 1).ToString().PadLeft(2, '0');
                strColNo2 = "001";
            }
            else
            {
                strColNoMillion2 = strColNoMillion;
                strColNo2 = (colNo + 1).ToString().PadLeft(3, '0');
            }
            strTfbhEastNorth = rowNoMillion2.ToString() + strColNoMillion2 + scale + strRowNo2 + strColNo2;
            return strTfbhEastNorth;
        }

        /// <summary>
        /// 获取某中小比例尺图号的西南图号
        /// </summary>
        /// <param name="strTfbh">图号</param>
        /// <param name="strScale">比例尺1:5000至1：250000</param>
        /// <returns></returns>
        public static string GetWestSouthTfbh(string strTfbh, string strScale)
        {
            int rowColCount = 0;
            string strTfbhWestSouth = "";
            double dblLong, dblLat;
            char rowNoMillion, rowNoMillion2;
            string strColNoMillion = "", strRowNo = "", strColNo = "";
            string strColNoMillion2 = "", strRowNo2 = "", strColNo2 = "";
            int rowNo, colNo;
            int colNoMillion = 0;
            char scale;
            switch (strScale)
            {
                case "1:5000":
                    rowColCount = 192;
                    scale = 'H';
                    if (!CheckTfbh5000(strTfbh, out  rowNoMillion, out strColNoMillion, out strRowNo, out strColNo, out dblLong, out dblLat)) return "";
                    break;
                case "1:10000":
                    rowColCount = 96;
                    scale = 'G';
                    if (!CheckTfbh10000(strTfbh, out  rowNoMillion, out strColNoMillion, out strRowNo, out strColNo, out dblLong, out dblLat)) return "";
                    break;
                case "1:50000":
                    rowColCount = 24;
                    scale = 'E';
                    if (!CheckTfbh50000(strTfbh, out  rowNoMillion, out strColNoMillion, out strRowNo, out strColNo, out dblLong, out dblLat)) return "";
                    break;
                case "1:250000":
                    rowColCount = 4;
                    scale = 'C';
                    if (!CheckTfbh250000(strTfbh, out  rowNoMillion, out strColNoMillion, out strRowNo, out strColNo, out dblLong, out dblLat)) return "";
                    break;
                default:
                    return "";
            }
            rowNo = Convert.ToInt32(strRowNo);
            colNo = Convert.ToInt32(strColNo);

            if (rowNo == rowColCount)
            {
                if (rowNoMillion == 'A') return "";
                rowNoMillion2 = (char)(rowNoMillion - 1);
                strRowNo2 = "001";
            }
            else
            {
                rowNoMillion2 = rowNoMillion;
                strRowNo2 = (rowNo + 1).ToString().PadLeft(3, '0');
            }
            if (colNo == 1)
            {
                colNoMillion = Convert.ToInt32(strColNoMillion);
                if (colNoMillion == 1) return "";
                strColNoMillion2 = (colNoMillion - 1).ToString().PadLeft(2, '0');
                strColNo2 = rowColCount.ToString();
            }
            else
            {
                strColNoMillion2 = strColNoMillion;
                strColNo2 = (colNo - 1).ToString().PadLeft(3, '0');

            }
            strTfbhWestSouth = rowNoMillion2.ToString() + strColNoMillion2 + scale + strRowNo2 + strColNo2;
            return strTfbhWestSouth;
        }

        /// <summary>
        /// 获取某中小比例尺图号的东南图号
        /// </summary>
        /// <param name="strTfbh">图号</param>
        /// <param name="strScale">比例尺1:5000至1：250000</param>
        /// <returns></returns>
        public static string GetEastSouthTfbh(string strTfbh, string strScale)
        {
            int rowColCount = 0;
            string strTfbhEastSouth = "";
            double dblLong, dblLat;
            char rowNoMillion, rowNoMillion2;
            string strColNoMillion = "", strRowNo = "", strColNo = "";
            string strColNoMillion2 = "", strRowNo2 = "", strColNo2 = "";
            int rowNo, colNo;
            int colNoMillion = 0;
            char scale;
            switch (strScale)
            {
                case "1:5000":
                    rowColCount = 192;
                    scale = 'H';
                    if (!CheckTfbh5000(strTfbh, out  rowNoMillion, out strColNoMillion, out strRowNo, out strColNo, out dblLong, out dblLat)) return "";
                    break;
                case "1:10000":
                    rowColCount = 96;
                    scale = 'G';
                    if (!CheckTfbh10000(strTfbh, out  rowNoMillion, out strColNoMillion, out strRowNo, out strColNo, out dblLong, out dblLat)) return "";
                    break;
                case "1:50000":
                    rowColCount = 24;
                    scale = 'E';
                    if (!CheckTfbh50000(strTfbh, out  rowNoMillion, out strColNoMillion, out strRowNo, out strColNo, out dblLong, out dblLat)) return "";
                    break;
                case "1:250000":
                    rowColCount = 4;
                    scale = 'C';
                    if (!CheckTfbh250000(strTfbh, out  rowNoMillion, out strColNoMillion, out strRowNo, out strColNo, out dblLong, out dblLat)) return "";
                    break;
                default:
                    return "";
            }
            rowNo = Convert.ToInt32(strRowNo);
            colNo = Convert.ToInt32(strColNo);

            if (rowNo == rowColCount)
            {
                if (rowNoMillion == 'A') return "";
                rowNoMillion2 = (char)(rowNoMillion - 1);
                strRowNo2 = "001";
            }
            else
            {
                rowNoMillion2 = rowNoMillion;
                strRowNo2 = (rowNo + 1).ToString().PadLeft(3, '0');
            }
            if (colNo == rowColCount)
            {
                colNoMillion = Convert.ToInt32(strColNoMillion);
                if (colNoMillion == 60) return "";
                strColNoMillion2 = (colNoMillion + 1).ToString().PadLeft(2, '0');
                strColNo2 = "001";
            }
            else
            {
                strColNoMillion2 = strColNoMillion;
                strColNo2 = (colNo + 1).ToString().PadLeft(3, '0');
            }

            strTfbhEastSouth = rowNoMillion2.ToString() + strColNoMillion2 + scale + strRowNo2 + strColNo2;
            return strTfbhEastSouth;
        }

        /// <summary>
        /// 获取点位置处（平面坐标）的大比例尺图幅的图号
        /// </summary>
        /// <param name="dblX"></param>
        /// <param name="dblY"></param>
        /// <param name="iPrecision"></param>
        /// <param name="width"></param>
        /// <param name="height"></param>
        /// <param name="dblNewXMin"></param>
        /// <param name="dblNewYMin"></param>
        /// <returns></returns>
        public static string Point2MapNo(double dblX,double dblY, int iPrecision, int width, int height, out  double dblLeftBottomX, out double dblLeftBottomY)
        {
            dblLeftBottomX = 0;
            dblLeftBottomY=0;            
            if (dblX % width == 0)
            {
                dblLeftBottomX = dblX;
            }
            else
            {
                if (dblX >= 0)
                    dblLeftBottomX = ((int)(dblX / width)) * width;
                else
                    dblLeftBottomX = ((int)(dblX / width) - 1) * width;
            }
            if (dblY % height == 0)
            {
                dblLeftBottomY = dblY;
            }
            else
            {
                if (dblY >= 0)
                    dblLeftBottomY = ((int)(dblY / height)) * height;
                else
                    dblLeftBottomY = ((int)(dblY / height) - 1) * height;
            }       
            dblX = dblLeftBottomX / 1000;
            dblY = dblLeftBottomY / 1000;
            return dblY.ToString("F" + iPrecision) + "-" + dblX.ToString("F" + iPrecision);
        }

        /// <summary>
        /// 获取点位置处（地理坐标）的1：5000图幅的图号
        /// </summary>
        /// <param name="dblLong">经度</param>
        /// <param name="dblLat">纬度</param>
        /// <returns></returns>
        public static string Point2MapNo5000(double dblLong, double dblLat)
        {
            string strMapNo = "";
            if (dblLong > 180 || dblLong < 0) return null;
            if (dblLat > 90 || dblLat < 0) return null;
            char rowMillion;
            string strColMillion;
            int colMillion;
            string strRow, strCol;
            int row, col;
            char scale = 'H';
            rowMillion = (char)((char)(dblLat / 4) + 'A');
            colMillion = (int)(dblLong / 6) + 31;
            strColMillion = colMillion.ToString().PadLeft(2, '0');

            double dblDeltRow = 4.0 / 192;
            double dblDeltCol = 6.0 / 192;

            row = (int)(((rowMillion - 'A' + 1) * 4 - dblLat) / dblDeltRow) + 1;
            col = (int)((dblLong - (colMillion - 31) * 6) / dblDeltCol) + 1;
            strRow = row.ToString().PadLeft(3, '0');
            strCol = col.ToString().PadLeft(3, '0');
            strMapNo = rowMillion.ToString() + strColMillion + scale.ToString() + strRow + strCol;
            return strMapNo;
        }
        /// <summary>
        /// 获取点位置处（地理坐标）的1：10000图幅的图号
        /// </summary>
        /// <param name="dblLong">经度</param>
        /// <param name="dblLat">纬度</param>
        /// <returns></returns>
        public static  string Point2MapNo10000(double dblLong, double dblLat)
        {
            string strMapNo = "";
            if (dblLong > 180 || dblLong < 0) return null;
            if (dblLat > 90 || dblLat < 0) return null;
            char rowMillion;
            string strColMillion;
            int colMillion;
            string strRow, strCol;
            int row, col;
            char scale = 'G';
            rowMillion =(char ) ((char)(dblLat / 4) +'A');
            colMillion = (int)(dblLong / 6)+31;
            strColMillion = colMillion.ToString().PadLeft(2, '0');

            double dblDeltRow = 4.0 / 96;
            double dblDeltCol = 6.0 / 96;

            row = (int)  (((rowMillion-'A'+1) * 4-dblLat ) / dblDeltRow )+1;
            col = (int)((dblLong - (colMillion-31) * 6) / dblDeltCol) +1;
            strRow = row.ToString().PadLeft(3, '0');
            strCol = col.ToString().PadLeft(3, '0');
            strMapNo = rowMillion.ToString() + strColMillion + scale.ToString() + strRow + strCol;
            return strMapNo;
        }

        /// <summary>
        /// 获取点位置处（地理坐标）的1：50000图幅的图号
        /// </summary>
        /// <param name="dblLong">经度</param>
        /// <param name="dblLat">纬度</param>
        /// <returns></returns>
        public static string Point2MapNo50000(double dblLong, double dblLat)
        {
            string strMapNo = "";
            if (dblLong > 180 || dblLong < 0) return null;
            if (dblLat > 90 || dblLat < 0) return null;
            char rowMillion;
            string strColMillion;
            int colMillion;
            string strRow, strCol;
            int row, col;
            char scale = 'E';
            rowMillion = (char)((char)(dblLat / 4) + 'A');
            colMillion = (int)(dblLong / 6) + 31;
            strColMillion = colMillion.ToString().PadLeft(2, '0');

            double dblDeltRow = 4.0 / 24;
            double dblDeltCol = 6.0 / 24;

            row = (int)(((rowMillion - 'A' + 1) * 4 - dblLat) / dblDeltRow) + 1;
            col = (int)((dblLong - (colMillion - 31) * 6) / dblDeltCol) + 1;
            strRow = row.ToString().PadLeft(3, '0');
            strCol = col.ToString().PadLeft(3, '0');
            strMapNo = rowMillion.ToString() + strColMillion + scale.ToString() + strRow + strCol;
            return strMapNo;
        }

        /// <summary>
        /// 获取点位置处（地理坐标）的1：250000图幅的图号
        /// </summary>
        /// <param name="dblLong">经度</param>
        /// <param name="dblLat">纬度</param>
        /// <returns></returns>
        public static string Point2MapNo250000(double dblLong, double dblLat)
        {
            string strMapNo = "";
            if (dblLong > 180 || dblLong < 0) return null;
            if (dblLat > 90 || dblLat < 0) return null;
            char rowMillion;
            string strColMillion;
            int colMillion;
            string strRow, strCol;
            int row, col;
            char scale = 'C';
            rowMillion = (char)((char)(dblLat / 4) + 'A');
            colMillion = (int)(dblLong / 6) + 31;
            strColMillion = colMillion.ToString().PadLeft(2, '0');

            double dblDeltRow = 4.0 / 4;
            double dblDeltCol = 6.0 / 4;

            row = (int)(((rowMillion - 'A' + 1) * 4 - dblLat) / dblDeltRow) + 1;
            col = (int)((dblLong - (colMillion - 31) * 6) / dblDeltCol) + 1;
            strRow = row.ToString().PadLeft(3, '0');
            strCol = col.ToString().PadLeft(3, '0');
            strMapNo = rowMillion.ToString() + strColMillion + scale.ToString() + strRow + strCol;
            return strMapNo;
        }
    }
}
